package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import dao.ExecuteQuery;


public class Register extends JPanel {
	private JTextField firstField;
	private JTextField lastField;
	private JTextField userField;
	private JTextField passField;
	private JTextField confirmField;
	private CardLayout c1;
	private JPanel basePanel;

	/**
	 * Create the panel.
	 */
	public Register(final CardLayout c1, final JPanel basePanel) 
	{
		this.c1 = c1;
		this.basePanel = basePanel;
		setName("GTBay New User Registration");
		setBackground(Color.WHITE);
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(19dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(31dlu;default):grow"),}));
		
		JLabel lblFirstName = new JLabel("First name");
		add(lblFirstName, "4, 2, left, default");
		
		firstField = new JTextField();
		add(firstField, "6, 2, fill, default");
		firstField.setColumns(10);
		
		JLabel label = new JLabel("    ");
		add(label, "8, 2");
		
		JLabel lblLastName = new JLabel("Last Name");
		add(lblLastName, "4, 6, left, default");
		
		lastField = new JTextField();
		add(lastField, "6, 6, fill, default");
		lastField.setColumns(10);
		
		JLabel lblUsername = new JLabel("Username");
		add(lblUsername, "4, 10, left, default");
		
		userField = new JTextField();
		add(userField, "6, 10, fill, default");
		userField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		add(lblPassword, "4, 14, left, default");
		
		passField = new JTextField();
		add(passField, "6, 14, fill, default");
		passField.setColumns(10);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password");
		add(lblConfirmPassword, "4, 18, left, default");
		
		confirmField = new JTextField();
		add(confirmField, "6, 18, fill, default");
		confirmField.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		add(panel, "6, 20, fill, fill");
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		panel.add(Box.createRigidArea(new Dimension(150, 0)));
		
		JButton btnCancel = new JButton("Cancel");
		panel.add(btnCancel);
		btnCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				c1.first(basePanel);
			}
		});
		panel.add(Box.createRigidArea(new Dimension(20, 0)));
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event)
			{
				String fName = firstField.getText();
				String lName = lastField.getText();
				String uName = userField.getText();
				String pass = passField.getText();
				String confirmPass = confirmField.getText();
				
				if (fName.equals("") || lName.equals("") || uName.equals("")
					|| pass.equals("") || confirmPass.equals(""))
						JOptionPane.showMessageDialog(null, "Please fill out all fields");
				
				else {
					if (!pass.equals(confirmPass))
						JOptionPane.showMessageDialog(null, "Password fields do not match");
					else {
						ExecuteQuery.register(fName, lName, uName, pass);
						JOptionPane.showMessageDialog(null, "Registration completed");
						c1.first(basePanel);
						}
					}
				}
			
		});
		panel.add(btnRegister);
	}

}
